package com.jie.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jie.commonutils.JwtUtils;
import com.jie.commonutils.MD5;
import com.jie.educenter.entity.LoginMember;
import com.jie.educenter.entity.UcenterMember;
import com.jie.educenter.entity.vo.RegisterVO;
import com.jie.educenter.mapper.UcenterMemberMapper;
import com.jie.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.servicebase.exception.GuLiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author xzj
 * @since 2020-10-15
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    private final RedisTemplate<String, String> redisTemplate;

    @Autowired
    public UcenterMemberServiceImpl(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public String login(LoginMember member) {

        // 获取登陆手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        // 手机号h和密码非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuLiException(20001, "登陆失败!");
        }

        // 判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        if (mobileMember == null) {  // 没有这个手机号
            throw new GuLiException(20001, "登陆失败! 账户或密码错误!");
        }

        // 判断密码是否正确(先加密再比较)
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            throw new GuLiException(20001, "登陆失败! 账户或密码错误!");
        }

        // 判断用户是否被禁用
        if (mobileMember.getIsDeleted()) {
            throw new GuLiException(20001, "登陆失败! 用户被禁用!");
        }

        // 登陆成功
        // 生成token, 使用jwt
        return JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
    }

    @Override
    public void register(RegisterVO registerVO) {

        // 获取注册的数据
        String code = registerVO.getCode();          // 获取验证码
        String mobile = registerVO.getMobile();      // 获取用户手机号
        String nickname = registerVO.getNickname();  // 获取用户昵称
        String password = registerVO.getPassword();  // 获取用户密码

        // 非空判断
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)) {
            throw new GuLiException(20001, "注册失败!");
        }

        // 判断验证码是否正确(从redis中取值)
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)) {
            throw new GuLiException(20001, "验证码错误! 注册失败!");
        }
        redisTemplate.delete(mobile);  // 清除掉验证码

        // 判断手机号是否重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            throw new GuLiException(20001, "手机号重复! 注册失败!");
        }

        // 插入数据到数据库
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));  // 密码需要加密
        member.setIsDisabled(false);  // 用户不禁用
        member.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        baseMapper.insert(member);

    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openid);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public Integer countRegisterDay(String date) {
        return baseMapper.countRegisterDay(date);
    }

}
